//
//  MWShareView.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/12/8.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWShareView.h"

@interface MWShareView ()

@property(nonatomic, strong) UIView *backgroundView;
@property(nonatomic, strong) UIView *contentView;

@property(nonatomic, strong) UIButton *wxShareBtn;
@property(nonatomic, strong) UIButton *friendShareBtn;
@property(nonatomic, strong) UIButton *facebookShareBtn;

@property(nonatomic, strong) UIButton *cancelBtn;

@end

@implementation MWShareView

- (instancetype)initWithFrame:(CGRect)frame {
    if (!(self =[super initWithFrame:[[UIScreen mainScreen] bounds]])) {
        return nil;
    }

    [self setupPageSubviews];
    
    return self;
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    _backgroundView.alpha = 0.0;
    MWWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backgroundView.alpha = 0.6;
        weakSelf.contentView.frame = CGRectMake(0, kScreenHeight - 250, kScreenWidth, 250);
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismiss {
    MWWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backgroundView.alpha = 0.0;
        weakSelf.contentView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 250);
    
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        
    }];
}

- (void)shareAction:(UIButton *)sender {
    if (self.block) {
        self.block(sender.tag);
    }
    
    [self dismiss];
}

- (void)cancelAction:(UIButton *)sender {
    [self dismiss];
}

- (void)setupPageSubviews {
    _backgroundView = [[UIView alloc] initWithFrame:self.frame];
    _backgroundView.backgroundColor = [UIColor blackColor];
    _backgroundView.alpha = 0.0;
    [self addSubview:_backgroundView];
    
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 250)];
    _contentView.backgroundColor = MWHexColor(@"#F7F7F7");
    [self addSubview:_contentView];
    
    _wxShareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _wxShareBtn.tag = 0;
    _wxShareBtn.frame = CGRectMake(0, 30, kScreenWidth / 3, 80);
    _wxShareBtn.titleLabel.font = MWFont(12);
    [_wxShareBtn setTitle:[[MWConfigManager manager] objectForKey:@"wx_friend" defaultValue:@"微信好友"] forState:UIControlStateNormal];
    [_wxShareBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_wxShareBtn setImage:[UIImage imageNamed:@"ic_wx"] forState:UIControlStateNormal];
    [_wxShareBtn setIconInTopWithSpacing:10];
    [_contentView addSubview:_wxShareBtn];
    
    _friendShareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _friendShareBtn.tag = 1;
    _friendShareBtn.frame = CGRectMake(kScreenWidth / 3, 30, kScreenWidth / 3, 80);
    _friendShareBtn.titleLabel.font = MWFont(12);
    [_friendShareBtn setTitle:[[MWConfigManager manager] objectForKey:@"quan" defaultValue:@"朋友圈"] forState:UIControlStateNormal];
    [_friendShareBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_friendShareBtn setImage:[UIImage imageNamed:@"ic_friend"] forState:UIControlStateNormal];
    [_friendShareBtn setIconInTopWithSpacing:10];
    [_contentView addSubview:_friendShareBtn];
    
    _facebookShareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _facebookShareBtn.tag = 2;
    _facebookShareBtn.frame = CGRectMake(kScreenWidth / 3 * 2, 30, kScreenWidth / 3, 80);
    _facebookShareBtn.titleLabel.font = MWFont(12);
    [_facebookShareBtn setTitle:@"Facebook" forState:UIControlStateNormal];
    [_facebookShareBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_facebookShareBtn setImage:[UIImage imageNamed:@"ic_facebook"] forState:UIControlStateNormal];
    [_facebookShareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [_facebookShareBtn setIconInTopWithSpacing:10];
    [_contentView addSubview:_facebookShareBtn];
    
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelBtn.backgroundColor = [UIColor whiteColor];
    _cancelBtn.frame = CGRectMake(0, 200, kScreenWidth, 50);
    _cancelBtn.titleLabel.font = MWFont(15);
    [_cancelBtn setTitle:[[MWConfigManager manager] objectForKey:@"cancel" defaultValue:@"取消"] forState:UIControlStateNormal];
    [_cancelBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_cancelBtn];
    
    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
        _wxShareBtn.hidden = NO;
        _friendShareBtn.hidden = NO;
        [_wxShareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
        [_friendShareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        _wxShareBtn.hidden = YES;
        _friendShareBtn.hidden = YES;
    }
}


@end
